//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.model
{
	import __AS3__.vec.Vector;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	import reader.ApplicationFacade;
	import reader.core.ReaderModelLocator;
	import reader.dao.IServiceDAO;
	import reader.model.vo.ServiceVO;

	public class ServiceProxy extends Proxy implements IServiceProxy
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "ServiceProxy";
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var serviceDAO:IServiceDAO;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function ServiceProxy()
		{
			super(NAME, []);
			serviceDAO = readerModelLocator.sqliteDAOFactory.serviceDAO;
		}
		
		public function addService(service:ServiceVO):void
		{
			if(serviceDAO.insertService(service, readerModelLocator.user) > 0)
			{
				sendNotification(ApplicationFacade.FETCH_SERVICES);
			}
		}
		
		public function updateService(service:ServiceVO):void
		{
			serviceDAO.updateService(service);
		}
		
		public function deleteService(service:ServiceVO):void
		{
			serviceDAO.deleteService(service);
		}
		
		public function selectService(sid:int):ServiceVO
		{
			return serviceDAO.selectService(sid);
		}
		
		public function selectAllService():Vector.<ServiceVO>
		{
			return serviceDAO.selectAllServices(readerModelLocator.user);
		}
		
		public function requestAuthWindow(service:ServiceVO, callback:Function):void
		{
			sendNotification(ApplicationFacade.OPEN_AUTH_WINDOW, {service: service, callback: callback});
		}
		
		public function deleteAccount():void
		{
			serviceDAO.deleteServiceByUser(readerModelLocator.user);
		}
	}
}